home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1994 August / August CD.bin / Shareware / Education / numericalmethods Folder / chap_5 / lspoly.m < prev    next >
Encoding:
Text File  |  1994-06-05  |  571 b   |  18 lines  |  [MATF/MATL]

  1. function C = lspoly(X,Y,m)
  2. % C = lspoly(X,Y)
  3. % Construct the least squares polynomial.
  4. % X is an 1xn abscissa vector, input.
  5. % Y is an 1xn ordinate vector, input.
  6. % m is the degree of the polynomial, input.
  7. % C is a coefficient list for the polynomial, output.
  8. n = length(X);
  9. B = zeros(1:m+1);
  10. F = zeros(n,m+1);
  11. for k=1:m+1,          % Powers of Xk are in columns of F.
  12.   F(:,k) = X'.^(k-1);
  13. end
  14. A = F'*F;             % Compute the matrix A.
  15. B = F'*Y';            % Compute the column vector B.
  16. C = A\B;              % Solve the linear system A*C=B for C.
  17. C = flipud(C);
  18.